package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojo.ApUser;
import com.heima.model.wemedia.dtos.WmLoginDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Override
    public ResponseResult login(WmLoginDto dto) {
       //TODO
        //查看是否包含该用户或密码
        WmUser wmUser = this.lambdaQuery().eq(WmUser::getName, dto.getName())
                .eq(WmUser::getPassword,dto.getPassword())
                .one();
        String token=new String();
        Map<String,Object> map = new HashMap<>();
        //不包含则有课游客方式，jwt为0
        if(wmUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }else{
            //包含则生成用jwt生成token返回
            token=AppJwtUtil.getToken(Long.valueOf(wmUser.getId()));
            map.put("user",wmUser);
            map.put("token",token);
        }
        return ResponseResult.okResult(map);

    }
}